package com.bifrost.config;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties;
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.hibernate4.LocalSessionFactoryBean;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.Database;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.persistence.EntityManager;
import javax.sql.DataSource;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * @author rangf
 * @date 2018/2/24
 */
@Order(Ordered.HIGHEST_PRECEDENCE)
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
        entityManagerFactoryRef="mainEntityManagerFactory",transactionManagerRef="transactionManagerPrimary",
        basePackages= {"com.bifrost.repository","com.bifrost.*.repository"})
//@EntityScan(basePackages = {"com.bifrost.entity","com.bifrost.*.entity"})
public class MainRepositoryConfig {

    @Value("${spring.jpa.hibernate.ddl-auto:#{none}}")
    private String ddlAuto;
    @Autowired
    private JpaProperties jpaProperties;

    @Bean
    public JpaProperties jpaProperties(){
        return new JpaProperties();
    }

    @Autowired
    @Qualifier("dataSource")
    private DataSource dataSource;

    @Bean(name = "entityManager")
    @Primary
    public EntityManager entityManager(EntityManagerFactoryBuilder builder) {
        return entityManagerFactory(builder).getObject().createEntityManager();
    }

    @Bean(name = "mainEntityManagerFactory")
    @Primary
    public LocalContainerEntityManagerFactoryBean entityManagerFactory (EntityManagerFactoryBuilder builder) {
        return builder
                .dataSource(dataSource)
                .properties(getVendorProperties(dataSource))
                .packages("com.bifrost.*.entity","com.bifrost.entity")
                .persistenceUnit("persistenceUnit")
                .build();
    }

    private Map<String, String> getVendorProperties(DataSource dataSource) {
        jpaProperties.setDatabase(Database.MYSQL);
        jpaProperties.getProperties().put("hibernate.dialect","org.hibernate.dialect.MySQL5Dialect");
        jpaProperties.getProperties().put("hibernate.hbm2ddl.auto",ddlAuto==null?"none":ddlAuto);
        return jpaProperties.getHibernateProperties(dataSource);
    }

    @Bean(name = "transactionManagerPrimary")
    @Primary
    PlatformTransactionManager transactionManagerPrimary(EntityManagerFactoryBuilder builder) {
        return new JpaTransactionManager(entityManagerFactory(builder).getObject());
    }

    @Bean
    PersistenceExceptionTranslationPostProcessor persistenceExceptionTranslationPostProcessor() {
        return new PersistenceExceptionTranslationPostProcessor();
    }
  /* @Bean(name = "sessionFactory")
   @Primary
   @Qualifier("sessionFactory")
   public SessionFactory sessionFactoryBean() {
       try {
           LocalSessionFactoryBean lsfb = new LocalSessionFactoryBean();
           lsfb.setDataSource(dataSource);
           lsfb.setPackagesToScan("com.bifrost.entity","com.bifrost.wechat.entity");
           Properties props = new Properties();
           props.setProperty("hibernate.show_sql", "false");
           props.setProperty("hibernate.hbm2ddl.auto", "none");
           props.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
           lsfb.setHibernateProperties(props);
           lsfb.afterPropertiesSet();
           SessionFactory object = lsfb.getObject();
           return object;
       } catch (IOException e) {
           return null;
       }
   }*/

}
